package com.haizhi.crm.sys.auth.dao;

import com.haizhi.crm.sys.auth.model.ResourceDo;
import com.haizhi.crm.sys.auth.model.ResourceVo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by yangyijun on 2018/5/10.
 */
@Repository
public interface ResourceDao extends JpaRepository<ResourceDo, Long> {
    @Query(value = "select id,name,remark,parentId from ResourceDo" )
    List<ResourceVo> list();

    @Query(value = "select id from ResourceDo" )
    List<Long> findIds();

    @Query(value = "select id from ResourceDo WHERE parentId=:parentId" )
    List<Long> findByParentId(@Param("parentId" ) Long parentId);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.ResourceVo" +
            "(r.id,r.name,r.remark,r.parentId,rrp.checked) " +
            "from PositionDo p,PositionRoleDo pr,RoleResourcePermissionDo rrp,ResourceDo r " +
            "where p.id=pr.positionId and pr.roleId=rrp.roleId " +
            "and rrp.resourceId=r.id and p.id=:positionId order by r.id " )
    List<ResourceVo> findByPositionId(@Param("positionId" ) Long positionId);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.ResourceVo" +
            "(res.id,res.name,res.remark,res.parentId,rrp.checked) " +
            "from RelationDo rel,RelationRoleDo rr,RoleResourcePermissionDo rrp,ResourceDo res " +
            "where rel.id=rr.relationId and rr.roleId=rrp.roleId " +
            "and rrp.resourceId=res.id and rel.id=:relationId order by res.id " )
    List<ResourceVo> findByRelationId(@Param("relationId" ) Long relationId);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.ResourceVo" +
            "(r.id,r.name,r.remark,r.parentId,rrp.checked) " +
            "from UserDo u,UserRoleDo ur,RoleResourcePermissionDo rrp,ResourceDo r " +
            "where u.id=ur.userId and ur.roleId=rrp.roleId " +
            "and rrp.resourceId=r.id and u.id=:userId and r.parentId=:parentId and rrp.checked='true'" )
    List<ResourceVo> findByUserId(@Param("userId" ) Long userId, @Param("parentId" ) Long parentId);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.ResourceVo" +
            "(r.id,r.name,r.remark,r.parentId,rrp.checked) " +
            "from UserDo u,UserRoleDo ur,RoleResourcePermissionDo rrp,ResourceDo r " +
            "where u.id=ur.userId and ur.roleId=rrp.roleId " +
            "and rrp.resourceId=r.id and u.id=:userId and rrp.checked='true'" )
    List<ResourceVo> findByUserId(@Param("userId" ) Long userId);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.ResourceVo" +
            "(r.id,r.name,r.remark,r.parentId,rrp.checked) " +
            "from UserDo u,UserRoleDo ur,RoleResourcePermissionDo rrp,ResourceDo r " +
            "where u.id=ur.userId and ur.roleId=rrp.roleId " +
            "and rrp.resourceId=r.id and u.id=:userId and rrp.checked='true' " +
            "and rrp.roleId not in (1,2,3)" )
    List<ResourceVo> findPositionResourcesByUserId(@Param("userId" ) Long userId);

    @Query(value = "select new com.haizhi.crm.sys.auth.model.ResourceVo" +
            "(r.id,r.name,r.remark,r.parentId,rrp.checked) " +
            "from UserDo u,UserRoleDo ur,RoleResourcePermissionDo rrp,ResourceDo r " +
            "where u.id=ur.userId and ur.roleId=rrp.roleId " +
            "and rrp.resourceId=r.id and u.id=:userId and rrp.checked='true' " +
            "and rrp.roleId in :roleIds" )
    List<ResourceVo> findRelationResourcesBy(@Param("userId" ) Long userId, @Param("roleIds" ) Long[] roleIds);
}
